package frame;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JToggleButton;
import javax.swing.table.DefaultTableModel;

import system.StudentSystem;
import dataBase.Data;

@SuppressWarnings("serial")
public class StudentFrame extends MainFrame {
//	private StudentSystem stuSys = null;
	private JToggleButton[] btnFunctions = new JToggleButton[3];
	private String[] functionName = new String[] { "Show All Course & Select",
			"Show My Course & Quit" };

	private JPanel selectPanel = new JPanel();
	private JPanel quitPanel = new JPanel();

	private JButton btnShowCourselist = new JButton("Show All Course");
	private JButton btnSelect = new JButton("Select Course");
	private JButton btnShowMyCourselist = new JButton("Show My CourseList");
	private JButton btnShowScore = new JButton("Show Score");
	private JButton btnQuit = new JButton("Quit Course");

	private JScrollPane tablePanel_1 = new JScrollPane();
	private JScrollPane tablePanel_2 = new JScrollPane();

	private DefaultTableModel dtm1 = new DefaultTableModel(18, 5);
	private DefaultTableModel dtm2 = new DefaultTableModel(18, 5);
	private JTable table2 = new JTable(dtm1) {
		public boolean isCellEditable(int row, int column) {
			return false;
		}
	};
	private JTable table1 = new JTable(dtm2) {
		public boolean isCellEditable(int row, int column) {
			return false;
		}
	};

	private boolean selectFlag = false;
	private boolean quitFlag = false;

	public StudentFrame(StudentSystem sys) {
		super(sys);

		for (int i = 0; i < functionName.length; i++) {
			btnFunctions[i] = new JToggleButton(functionName[i]);
			btnGroup.add(btnFunctions[i]);
			inScroll.add(btnFunctions[i]);
			btnFunctions[i].addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					JToggleButton btn = (JToggleButton) e.getSource();
					cl.show(rightPanel, btn.getText());
				}
			});
		}

		rightPanel.add(selectPanel, "Show My Course & Quit");
		selectPanel.setLayout(null);

		btnShowCourselist.addActionListener(new ShowAllActionListener());
		btnSelect.addActionListener(new SelectActionListener());
		btnShowMyCourselist.addActionListener(new ShowMyActionListener());
		btnQuit.addActionListener(new QuitActionListener());
		btnShowScore.addActionListener(new ShowScoreActionListener());

		JPanel btnPanel_1 = new JPanel();
		FlowLayout fl_btnPanel_1 = (FlowLayout) btnPanel_1.getLayout();
		fl_btnPanel_1.setHgap(50);
		btnPanel_1.setBounds(10, 60, 505, 47);
		selectPanel.add(btnPanel_1);

		btnPanel_1.add(btnShowMyCourselist);
		btnPanel_1.add(btnShowScore);
		btnPanel_1.add(btnQuit);

		tablePanel_1.setBounds(10, 130, 505, 313);
		selectPanel.add(tablePanel_1);

		rightPanel.add(quitPanel, "Show All Course & Select");
		quitPanel.setLayout(null);

		JPanel btnPanel_2 = new JPanel();
		FlowLayout flowLayout = (FlowLayout) btnPanel_2.getLayout();
		flowLayout.setHgap(40);
		btnPanel_2.setBounds(10, 60, 505, 47);
		quitPanel.add(btnPanel_2);

		btnPanel_2.add(btnShowCourselist);

		btnPanel_2.add(btnSelect);

		tablePanel_2.setBounds(10, 130, 505, 313);
		quitPanel.add(tablePanel_2);

		tablePanel_1.setViewportView(table2);
		tablePanel_2.setViewportView(table1);

		cl.show(rightPanel, "Index");
	}

	@SuppressWarnings("unchecked")
	public void showAll() {
		String cmd = "Show allCourse";
		command.run_gui(cmd);
		ArrayList<Data> l = (ArrayList<Data>) command.getResourse();
		((DefaultTableModel) table1.getModel()).getDataVector().clear();
		((DefaultTableModel) table1.getModel()).fireTableDataChanged();
		table2.updateUI();
		if (!l.isEmpty()) {
			dtm2.setColumnCount(l.get(0).getIdentifier().length);
			dtm2.setColumnIdentifiers(l.get(0).getIdentifier());
			int location = 0;
			for (Data d : l) {
				dtm2.insertRow(location, d.toArray());
				location++;
			}
			selectFlag = true;
		}
	}

	class ShowAllActionListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			showAll();
		}
	}

	class SelectActionListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			int flag = 0;
			try {
				if (selectFlag) {
					String tag = (String) table1.getValueAt(
							table1.getSelectedRow(), 0);
					String cmd = "Select course " + tag;
					flag = command.run_gui(cmd);
					if(flag == 0) {
						JOptionPane.showMessageDialog(btnSelect, "Select course succeed.");
					} else {
						JOptionPane.showMessageDialog(btnSelect, "Unknown Error.");
					}
					selectFlag = false;
					showAll();
				}
			} catch (ArrayIndexOutOfBoundsException e1) {
				JOptionPane.showMessageDialog(btnSelect, "Before select, you should choose a course");
			}
			
		}
	}

	@SuppressWarnings("unchecked")
	public void showMy() {
		String cmd = "Show mycourseList";
		command.run_gui(cmd);
		ArrayList<Data> l = (ArrayList<Data>) command.getResourse();
		((DefaultTableModel) table2.getModel()).getDataVector().clear();
		((DefaultTableModel) table2.getModel()).fireTableDataChanged();
		table2.updateUI();
		if (!l.isEmpty()) {
			dtm1.setColumnCount(l.get(0).getIdentifier().length);
			dtm1.setColumnIdentifiers(l.get(0).getIdentifier());
			int location = 0;
			for (Data d : l) {
				dtm1.insertRow(location, d.toArray());
				location++;
			}
			quitFlag = true;
		}
	}
	class ShowMyActionListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			showMy();
		}
	}

	class QuitActionListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			int flag = 0;
			try {
				if (quitFlag) {
					String tag = (String) table2.getValueAt(
							table2.getSelectedRow(), 0);
					String cmd = "Quit course " + tag;
					flag = command.run_gui(cmd);
					quitFlag = false;
					if(flag == 0) {
						JOptionPane.showMessageDialog(btnQuit, "Quit course succeed.");
					} else {
						JOptionPane.showMessageDialog(btnQuit, "Unknown Error.");
					}
					showMy();
				}
			} catch (ArrayIndexOutOfBoundsException e1) {
				JOptionPane.showMessageDialog(btnSelect, "Before quit, you should choose a course");
			}
		}
	}

	class ShowScoreActionListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent arg0) {
			double flag;
			try {
				if (quitFlag) {
					String tag = (String) table2.getValueAt(
							table2.getSelectedRow(), 0);
					String cmd = "Show score " + tag;
					flag = command.run_gui(cmd);
					if (flag == -4) {
						JOptionPane.showMessageDialog(btnShowScore,
								"The score has not been recorded.");
					} else {
						double score = (double) command.getResourse();
						JOptionPane.showMessageDialog(btnShowScore, "Your score: " + score);
					}
				}
			} catch (ArrayIndexOutOfBoundsException e1) {
				JOptionPane.showMessageDialog(btnSelect, "Before show score, you should choose a course");
			}
			showMy();
		}
	}
	@Override
	public void runChangePasswordAction() {
		ChangePasswordFrame cf = new ChangePasswordFrame(this);
		cf.setVisible(true);
		cf.setAlwaysOnTop(true);
		setEnabled(false);
	}
}
